
package evocarcassonne.tileseteditor.ui.swing;

import evocarcassonne.core.abstractors.AbstractDrawableTile;
import evocarcassonne.core.abstractors.AbstractTileDrawer;
import evocarcassonne.tileseteditor.managers.TileManager;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;


public class TileDrawer extends AbstractTileDrawer {

    Timer timer;
    JPanel panel;

    private Graphics2D bufferedGraphics;

    public TileDrawer(JPanel drawingPanel) {
        panel = drawingPanel;
        panel.setDoubleBuffered(true);
    }

    public void repaint() {

        Image offscreenImage = panel.createImage(panel.getWidth(), panel.getHeight());
        bufferedGraphics = (Graphics2D) offscreenImage.getGraphics();
        draw(TileManager.getTile());

        Graphics2D mainGraphics = (Graphics2D) panel.getGraphics();
        mainGraphics.drawImage(offscreenImage, 0, 0, panel);
    }


    // IMPLEMENTED ABSTRACT METHODS

    @Override
    protected AbstractDrawableTile _generateMeadowGraphics() {
        return new SwingImage("resources/patterns/misc/field.png");
    }

    // cities
    @Override
    protected AbstractDrawableTile _generateSimpleCityGraphics() {
        return new SwingImage("resources/patterns/city/simple.png");
    }

    @Override
    protected AbstractDrawableTile _generateSideCityGraphics() {
        return new SwingImage("resources/patterns/city/side.png");
    }

    @Override
    protected AbstractDrawableTile _generateOppositeCityGraphics() {
        return new SwingImage("resources/patterns/city/opposite.png");
    }

    @Override
    protected AbstractDrawableTile _generateTripleCityGraphics() {
        return new SwingImage("resources/patterns/city/triple.png");
    }
    
    @Override
    protected AbstractDrawableTile _generateQuadCityGraphics() {
        return new SwingImage("resources/patterns/city/quad.png");
    }

    
    
    // roads
    @Override
    protected AbstractDrawableTile _generateSimpleRoadGraphics() {
        return new SwingImage("resources/patterns/road/simple.png");
    }

    @Override
    protected AbstractDrawableTile _generateSideRoadGraphics() {
        return new SwingImage("resources/patterns/road/side1.png");
    }

    @Override
    protected AbstractDrawableTile _generateOppositeRoadGraphics() {
        return new SwingImage("resources/patterns/road/opposite.png");
    }

    @Override
    protected AbstractDrawableTile _generateTouchRoadGraphics() {
        return new SwingImage("resources/patterns/road/touch.png");
    }

    @Override
    protected AbstractDrawableTile _generateDeadEndGraphics() {
        return new SwingImage("resources/patterns/road/deadend.png");
    }
    
    @Override
    protected AbstractDrawableTile _generateTripleRoadGraphics() {
        return new SwingImage("resources/patterns/road/circle3.png");
    }
    
    @Override
    protected AbstractDrawableTile _generateQuadRoadGraphics() {
        return new SwingImage("resources/patterns/road/circle4.png");
    }

    // rotations
        
    @Override
    protected void _draw0(AbstractDrawableTile abstractImage) {
        SwingImage image = (SwingImage) abstractImage;

        AffineTransform transformer = new AffineTransform();
        transformer.translate(panel.getWidth() / 2 - image.getWidth(panel) / 2, panel.getHeight() / 2 - image.getHeight(panel) / 2);

        bufferedGraphics.drawImage(image.getImage(), transformer, panel);
    }

    @Override
    protected void _draw90(AbstractDrawableTile abstractImage) {
        SwingImage image = (SwingImage) abstractImage;

        AffineTransform transformer = new AffineTransform();
        transformer.translate(panel.getWidth() / 2 - image.getWidth(panel) / 2, panel.getHeight() / 2 - image.getHeight(panel) / 2);
        transformer.rotate(Math.PI / 2);
        transformer.translate(0, -image.getHeight(panel));

        bufferedGraphics.drawImage(image.getImage(), transformer, panel);
    }

    @Override
    protected void _draw180(AbstractDrawableTile abstractImage) {
        SwingImage image = (SwingImage) abstractImage;

        AffineTransform transformer = new AffineTransform();
        transformer.translate(panel.getWidth() / 2 - image.getWidth(panel) / 2, panel.getHeight() / 2 - image.getHeight(panel) / 2);
        transformer.rotate(Math.PI);
        transformer.translate(-image.getWidth(panel), -image.getHeight(panel));

        bufferedGraphics.drawImage(image.getImage(), transformer, panel);
    }

    @Override
    protected void _draw270(AbstractDrawableTile abstractImage) {
        SwingImage image = (SwingImage) abstractImage;

        AffineTransform transformer = new AffineTransform();
        transformer.translate(panel.getWidth() / 2 - image.getWidth(panel) / 2, panel.getHeight() / 2 - image.getHeight(panel) / 2);
        transformer.rotate(-Math.PI / 2);
        transformer.translate(-image.getWidth(panel), 0);

        bufferedGraphics.drawImage(image.getImage(), transformer, panel);
    }
}


